﻿/* This is created to load the game
 * 11-SEP-12, Phu Truong, First Draft.
 * 14-SEP-12, Q. Nguyen, 
 * */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Air_Trafic_Control.Domain.Entity;
using Air_Trafic_Control.Domain.Control;
using Air_Trafic_Control.Domain.Common;
using Air_Trafic_Control.Domain.Persistence;
using Air_Trafic_Control.Domain.Util;

namespace Air_Trafic_Control
{
    public partial class ptLoadGame : Form
    {
        private EAT m_eat;
        private static Logger log = new Logger(typeof(ptLoadGame));

        #region Constructor

        public ptLoadGame()
        {
            // Get the Singleton Instance of EAT (the one and only)
            this.m_eat = EAT.GetEAT();

            // Initialize all Components
            InitializeComponent();

            // Ensure we have a user
            if (this.m_eat.GetUsers().Count > 0)
            {
                // Add the Users loaded from file by EAT to our list
                this.ptUserList.Items.AddRange(this.m_eat.GetUsers().ToArray());

                // Select the first Item by default
                this.ptUserList.SelectedItem = this.ptUserList.Items[0];

                // Set the selected user
                this.m_eat.SelectedUser = (User)this.ptUserList.SelectedItem;
            }
            else
            {
                this.ptUserList.Enabled = false;
                this.ptGameList.Enabled = false;
                this.ptLoad.Enabled = false;
            }
        } 

        #endregion

        #region Private Methods

        private void ptCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void ptUserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Update the selected user
            this.m_eat.SelectedUser = (User)this.ptUserList.SelectedItem;

            // Clear the Games list
            this.ptGameList.Items.Clear();

            // Set the Games list from what the EAT loaded
            this.ptGameList.Items.AddRange(this.m_eat.GetGamePersistenceHandler().Games.ToArray());
        }

        private void ptLoad_Click(object sender, EventArgs e)
        {
            // Set the User and Game in the EAT
            this.m_eat.SelectedUser = (User)this.ptUserList.SelectedItem;
            this.m_eat.SelectedGame = (Game)this.m_eat.GetGamePersistenceHandler().Games[this.ptGameList.SelectedIndex];

            // Close this window
            this.Dispose();
        }

        private void ptGameList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Enable the Load button
            this.ptLoad.Enabled = true;
        } 

        #endregion
    }
}
